package hung.vnptthanhhoa.util;

import hung.vnptthanhhoa.adapter.FoodList;

import java.util.ArrayList;

import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Base64;
import android.util.Log;

public class DBController extends SQLiteOpenHelper {

	public DBController(Context context) {
		super(context, "for_woman.db", null, 5);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		String query = "CREATE TABLE  forwoman_monan  ( id 	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,name TEXT,group_id INTEGER,pic BLOB,id_sync INTEGER,note varchar)";
		db.execSQL(query);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String query = "DROP TABLE IF EXISTS forwoman_monan";
		db.execSQL(query);
		onCreate(db);

	}

	public boolean insertMonAn(JSONObject queryValue) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValue = new ContentValues();
		try {
			Cursor cursor = getByIdSync(queryValue.getInt("id"));

			contentValue.put("name", queryValue.getString("name"));
			contentValue.put("group_id", queryValue.getInt("group_id"));
			contentValue.put("pic", queryValue.getString("data"));
			contentValue.put("id_sync", queryValue.getInt("id"));
			contentValue.put("note", queryValue.getString("note"));
			if (cursor.getCount() > 0) {

				db.update("forwoman_monan", contentValue, "id_sync = "
						+ queryValue.getInt("id"), null);

			} else {
				db.insert("forwoman_monan", "pic", contentValue);

			}
		} catch (Exception e) {
			Log.e("loi", e.toString());
			return false;
		}

		db.close();
		return true;
	}

	public Cursor getByIdSync(int id_sync) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"SELECT id FROM forwoman_monan where id_sync = " + id_sync
						+ " ", null);
		// db.close();
		return cursor;
	}

	public ArrayList<FoodList> getArrayListByGroupId(int group_id, int start,
			int limit) {

		ArrayList<FoodList> listMonAn = new ArrayList<FoodList>();
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"SELECT * FROM forwoman_monan WHERE group_id=" + group_id
						+ " LIMIT " + start + " , " + limit, null);
		if (cursor.moveToFirst()) {
			do {
				FoodList foodList = new FoodList();
				foodList.setId(Integer.parseInt(cursor.getString(0)));
				foodList.setName(cursor.getString(1));
				foodList.setImage(Base64.decode(cursor.getString(3), 0));
				foodList.settId_Sync(Integer.parseInt(cursor.getString(4)));
				foodList.setNote(cursor.getString(5));
				listMonAn.add(foodList);
			} while (cursor.moveToNext());
		}
		db.close();
		return listMonAn;
	}

	public Cursor getFoodByGroupId(int group_id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db
				.rawQuery("SELECT * FROM forwoman_monan WHERE group_id="
						+ group_id, null);
		// db.close();
		Log.e("22", String.valueOf(cursor.getCount()));
		db.close();
		return cursor;
	}

	public void deleteAll() {
		SQLiteDatabase db = this.getReadableDatabase();
		db.delete("forwoman_monan", null, null);
		db.close();
	}

	public int getCountByGroupId(int group_id) {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"SELECT count('id') as count FROM forwoman_monan where group_id = "
						+ group_id, null);
		//db.close();
		
		return cursor.getCount();
	}

}
